• File: gps_tracker_address_reverse.php
  • Full Path: C:/htdocs/reeft_gps_test/css/gps_tracker_address_reverse.php
  • Date Modified: 03/26/2025 10:16 AM
  • File size: 3.87 KB
  • MIME-type: text/x-php
  • Charset: utf-8
<?php
//======================================================================================
//
// Function: Resolve address from coordinate
// uses dataforsyningen.dk and gps-tracker as fallback
//
// Programmer: AR
// Date      : 2024-11-15
//
// Copyright Reeft A/S (c) - 2024
//======================================================================================

// https://login.gps-tracker.dk/api/address/reverse?lang=en&lng=11.754098&lat=55.21014&user_api_hash=$2y$10$SC.XMMJovzAiHijfPf/0F.AAatv6EBPw.4S0DbyQCUHNErB7qfd4q

// https://dawadocs.dataforsyningen.dk/dok/api/adgangsadresse#reverse

//======================================================================================
// General 
//======================================================================================
include "include/apikey.php";

//======================================================================================
// Get input
//======================================================================================
if (isset($_REQUEST["lat"]))    $lat = $_REQUEST["lat"];
else $lat = 0;

if (isset($_REQUEST["lng"]))    $lng = $_REQUEST["lng"];
else $lng = 0;

// call 'dataforsyningen'
$parms 	= '?x=' . $lng
		. '&y=' . $lat
		. '&struktur=mini'
		;


// Set URL
$url = $dataforsyningenUrl . '/' . 'adgangsadresser/reverse';	

// Add parms
$url = $url . $parms;
//	echo $url;

// Create a new cURL resource
$ch = curl_init($url);

// Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));	

// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_HEADER, false);    // we do not need headers
curl_setopt($ch, CURLOPT_NOBODY, false);    // we don't need body

// Execute the GET request
$result = curl_exec($ch);

$error_msg = "";
if (curl_errno($ch)) {
	$error_msg = curl_error($ch);
}

// Close cURL resource
curl_close($ch);

$dataReturn = json_decode($result, true);
$returnResult = [];

$returnResult["error"]	= $error_msg;

if (isset($dataReturn["id"])) {
	$returnResult["data"] = [];
	$returnResult["data"]["location"] = [];
	$returnResult["data"]["location"]["house"] 	= $dataReturn["husnr"];
	$returnResult["data"]["location"]["road"] 	= $dataReturn["vejnavn"];
	$returnResult["data"]["location"]["city"] 	= $dataReturn["postnrnavn"];
	$returnResult["data"]["location"]["zip"] 	= $dataReturn["postnr"];
} else {
	$returnError = @$dataReturn["title"];
	$returnErrorDetails = @$dataReturn["details"];
	$returnResult["error"]	.= " " .$returnError; 
	$returnResult["data"] 	= "";
	
	// Get location from gps-tracker
	$parms 	= '?' 
		. 'lang=en'
		. '&lat=' . $lat
		. '&lng=' . $lng
		. '&user_api_hash=' . $gsmKey
		;

	// Set URL
	$url = $dataforsyningenUrl . '/' . 'address/reverse';	

	// Add parms
	$url = $url . $parms;

	//	echo $url;

	// Create a new cURL resource
	$ch = curl_init($url);

	// Set the content type to application/json
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));	

	// Return response instead of outputting
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

	curl_setopt($ch, CURLOPT_POST, false);
	curl_setopt($ch, CURLOPT_HEADER, false);    // we do not need headers
	curl_setopt($ch, CURLOPT_NOBODY, false);    // we don't need body

	// Execute the GET request
	$result = curl_exec($ch);

	$error_msg = "";
	if (curl_errno($ch)) {
		$error_msg = curl_error($ch);
	}

	// Close cURL resource
	curl_close($ch);

	$data = json_decode($result, true);
	
	$returnResult["error"]	.= " " . $error_msg;
	
	if ($data["status"] != 1) {
		$returnResult["error"]	.= " " .$data["message"]; 
		$returnResult["data"] 	= "";
	} else {
		$returnResult["data"] 	= $data;	
	}
}

echo json_encode($returnResult);